package com.mldong.modules.sys.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Dict;
import com.mldong.base.CommonResult;
import com.mldong.modules.sys.dto.LoginParam;
import com.mldong.modules.sys.service.UserService;
import com.mldong.modules.sys.vo.LoginVO;
import com.mldong.validation.Groups;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "权限相关")
@RequiredArgsConstructor
public class AuthController {
    private final UserService userService;
    /**
     * 登录
     * @param param
     * @return
     */
    @PostMapping("/sys/login")
    @ApiOperation(value = "登录")
    public CommonResult<LoginVO> login(@RequestBody @Validated() LoginParam param) {

        return CommonResult.data(userService.login(param));
    }
    /**
     * 退出
     * @param authorization
     * @return
     */
    @PostMapping("/sys/logout")
    @ApiOperation(value = "退出")
    public CommonResult<?> logout(@RequestHeader String authorization) {
        String token = authorization.replace("Bearer ","");
        userService.logout(token);
        return CommonResult.ok();
    }
    /**
     * 获取登录图片验证码是否启用标识
     * @return
     */
    @PostMapping("/sys/getCaptchaOpenFlag")
    @ApiOperation(value = "获取登录图片验证码是否启用标识")
    public CommonResult<?> getCaptchaOpenFlag(){
        Dict dict = Dict.create();
        dict.set("flag", false);
        return CommonResult.data(dict);
    }
    /**
     * 扮演用户
     * @param param
     * @return
     */
    @PostMapping("/sys/playUser")
    @ApiOperation(value = "扮演用户")
    public CommonResult<LoginVO> playUser(@RequestBody @Validated({LoginParam.PlayUser.class}) LoginParam param) {
        return CommonResult.data(userService.playUser(param.getUserId()));
    }
    /**
     * 取消扮演
     * @param authorization
     * @return
     */
    @PostMapping("/sys/unPlayUser")
    @ApiOperation(value = "取消扮演")
    public CommonResult<LoginVO> unPlayUser(@RequestHeader String authorization) {
        String token = authorization.replace("Bearer ","");
        return CommonResult.data(userService.unPlayUser(token));
    }
    @PostMapping("/testSaToken")
    @ApiOperation(value = "检验sa-token是否登录")
    public CommonResult<SaTokenInfo> testSaToken(){
        StpUtil.checkLogin();
        return CommonResult.data(StpUtil.getTokenInfo());
    }
}
